//
//  MouseItemView.swift
//  whack-a-mole
//
//  Created by wxc on 2019/9/19.
//  Copyright © 2019 wxc. All rights reserved.
//

import UIKit
import AVFoundation

class MouseItemView: UIView {
    
    weak var delegate: MouseItemViewDelegate?
    var player : AVAudioPlayer?

    private lazy var holeIMG : UIImageView = {
        let holeIMG = UIImageView.init()
        holeIMG.frame = CGRect.init(x: 5, y: self.zc_height - self.zc_height/3, width: self.zc_width - 10, height: self.zc_height/3 + 20)
        holeIMG.contentMode = .scaleToFill
        holeIMG.image = UIImage.init(named: "土坑")
        return holeIMG
    }()
    
    private lazy var mouseIMG : UIImageView = {
        let mouseIMG = UIImageView.init()
        mouseIMG.frame = CGRect.init(x: self.zc_width/3, y: self.zc_height - self.zc_height/3, width: self.zc_width/3, height: self.zc_height - self.zc_height/3 - 20)
        mouseIMG.image = UIImage.init(named: "地鼠")
        mouseIMG.isUserInteractionEnabled = true
        mouseIMG.addGestureRecognizer(UIGestureRecognizer.init(target: self, action: #selector(hitMouseItem)))
        return mouseIMG
    }()
    
    private lazy var hitSound : SoundTool = {
       let hitSound = SoundTool.init()
        return hitSound
    }() // 声音
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.clipsToBounds = true
        self.addSubview(self.mouseIMG)
        self.addSubview(self.holeIMG)
    }
    
    func refreshUI() -> Void {
        self.mouseIMG.frame = CGRect.init(x: self.zc_width/3, y: self.zc_height - self.zc_height/3, width: self.zc_width/3, height: self.zc_height - self.zc_height/3 - 20)
        self.holeIMG.frame = CGRect.init(x: 5, y: self.zc_height - self.zc_height/3, width: self.zc_width - 10, height: self.zc_height/3 + 20)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func hitMouseOn() -> Void {
//        hitMouseSound()
        self.mouseIMG.image = UIImage.init(named: "挨打地鼠")
        hitMouseInHole()
    }
    
    func hitMouseSound() -> Void {
        let pathString = Bundle.main.path(forResource: "First_Tone", ofType: "mp3")
        let url = URL.init(fileURLWithPath: pathString!)
        playSoundWithURL(soundURL: url)
    }
    func TaphitMouseSound(name:String) -> Void {
        let pathString = Bundle.main.path(forResource: name, ofType: "mp3")
        let url = URL.init(fileURLWithPath: pathString!)
        playSoundWithURL(soundURL: url)
    }
    
    func playSoundWithURL(soundURL : URL) -> Void {
        
        DispatchQueue.main.async {
            do{
                 self.player = try AVAudioPlayer.init(contentsOf: soundURL)
             }catch {}
            
             self.player?.volume = 1
            self.player?.play()
        }
    
    }
    
    func hitMouseInHole() -> Void {
        UIView.animate(withDuration: TimeInterval.init(0.5), animations: {
            self.mouseIMG.zc_y = self.zc_height - self.zc_height/3
        }) { (isbool) in
            self.mouseIMG.image = UIImage.init(named: "地鼠")
        }
    }
    
    @objc func hitMouseItem() -> Void {
        if (self.delegate != nil) && (self.mouseIMG.zc_y < (self.zc_height - self.zc_height/3))
        {
            hitMouseOn()
            self.delegate?.hitMouse(mouseItem: self)
        }
    }
    
    func mouseOutHole() -> Void {
        UIView.animate(withDuration: 0.1) {
            self.mouseIMG.zc_y = 20
        }
    }
    
    func mouseInHole() -> Void {
        UIView.animate(withDuration: 0.1) {
            self.mouseIMG.zc_y = self.zc_height - self.zc_height/3
        }
    }
}

@objc protocol MouseItemViewDelegate {
    func hitMouse(mouseItem: MouseItemView?)
}
